package com.welson.restfultoolx.settings;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import java.awt.*;

/**
 * RestfulToolX设置面板
 * 提供插件配置界面
 */
public class RestfulToolXSettingsPanel {
    
    private JPanel mainPanel;
    private JCheckBox enableCacheCheckBox;
    private JSpinner maxCacheSizeSpinner;
    private JSpinner maxSearchResultsSpinner;
    private JCheckBox caseSensitiveSearchCheckBox;
    private JCheckBox includeDeprecatedCheckBox;
    private JCheckBox autoRefreshCheckBox;
    private JSpinner refreshIntervalSpinner;
    
    // 默认设置值
    private static final int DEFAULT_MAX_CACHE_SIZE = 1000;
    private static final int DEFAULT_MAX_SEARCH_RESULTS = 50;
    private static final int DEFAULT_REFRESH_INTERVAL = 30;
    
    public RestfulToolXSettingsPanel() {
        initComponents();
        reset(); // 加载默认值
    }
    
    private void initComponents() {
        mainPanel = new JPanel(new BorderLayout());
        
        // 创建设置面板
        JPanel settingsPanel = new JPanel();
        settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
        settingsPanel.setBorder(JBUI.Borders.empty(20));
        
        // 缓存设置组
        JPanel cacheGroup = createCacheSettingsGroup();
        settingsPanel.add(cacheGroup);
        settingsPanel.add(Box.createVerticalStrut(20));
        
        // 搜索设置组
        JPanel searchGroup = createSearchSettingsGroup();
        settingsPanel.add(searchGroup);
        settingsPanel.add(Box.createVerticalStrut(20));
        
        // 刷新设置组
        JPanel refreshGroup = createRefreshSettingsGroup();
        settingsPanel.add(refreshGroup);
        
        // 添加弹性空间
        settingsPanel.add(Box.createVerticalGlue());
        
        mainPanel.add(settingsPanel, BorderLayout.CENTER);
    }
    
    private JPanel createCacheSettingsGroup() {
        JPanel group = new JPanel();
        group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));
        group.setBorder(BorderFactory.createTitledBorder("Cache Settings"));
        
        // 启用缓存
        enableCacheCheckBox = new JCheckBox("Enable caching for better performance");
        group.add(enableCacheCheckBox);
        group.add(Box.createVerticalStrut(10));
        
        // 最大缓存大小
        JPanel cacheSizePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        cacheSizePanel.add(new JLabel("Maximum cache size:"));
        maxCacheSizeSpinner = new JSpinner(new SpinnerNumberModel(DEFAULT_MAX_CACHE_SIZE, 100, 10000, 100));
        maxCacheSizeSpinner.setPreferredSize(new Dimension(80, 25));
        cacheSizePanel.add(maxCacheSizeSpinner);
        cacheSizePanel.add(new JLabel("items"));
        group.add(cacheSizePanel);
        
        return group;
    }
    
    private JPanel createSearchSettingsGroup() {
        JPanel group = new JPanel();
        group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));
        group.setBorder(BorderFactory.createTitledBorder("Search Settings"));
        
        // 最大搜索结果数
        JPanel maxResultsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        maxResultsPanel.add(new JLabel("Maximum search results:"));
        maxSearchResultsSpinner = new JSpinner(new SpinnerNumberModel(DEFAULT_MAX_SEARCH_RESULTS, 10, 1000, 10));
        maxSearchResultsSpinner.setPreferredSize(new Dimension(80, 25));
        maxResultsPanel.add(maxSearchResultsSpinner);
        maxResultsPanel.add(new JLabel("items"));
        group.add(maxResultsPanel);
        group.add(Box.createVerticalStrut(10));
        
        // 区分大小写
        caseSensitiveSearchCheckBox = new JCheckBox("Case sensitive search by default");
        group.add(caseSensitiveSearchCheckBox);
        group.add(Box.createVerticalStrut(5));
        
        // 包含已废弃的接口
        includeDeprecatedCheckBox = new JCheckBox("Include deprecated APIs in search results");
        group.add(includeDeprecatedCheckBox);
        
        return group;
    }
    
    private JPanel createRefreshSettingsGroup() {
        JPanel group = new JPanel();
        group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));
        group.setBorder(BorderFactory.createTitledBorder("Auto Refresh Settings"));
        
        // 自动刷新
        autoRefreshCheckBox = new JCheckBox("Auto refresh API list when files change");
        group.add(autoRefreshCheckBox);
        group.add(Box.createVerticalStrut(10));
        
        // 刷新间隔
        JPanel intervalPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        intervalPanel.add(new JLabel("Refresh interval:"));
        refreshIntervalSpinner = new JSpinner(new SpinnerNumberModel(DEFAULT_REFRESH_INTERVAL, 5, 300, 5));
        refreshIntervalSpinner.setPreferredSize(new Dimension(80, 25));
        intervalPanel.add(refreshIntervalSpinner);
        intervalPanel.add(new JLabel("seconds"));
        group.add(intervalPanel);
        
        // 启用/禁用刷新间隔设置
        autoRefreshCheckBox.addActionListener(e -> {
            boolean enabled = autoRefreshCheckBox.isSelected();
            refreshIntervalSpinner.setEnabled(enabled);
        });
        
        return group;
    }
    
    public JComponent getMainPanel() {
        return mainPanel;
    }
    
    public boolean isModified() {
        RestfulToolXSettings settings = RestfulToolXSettings.getInstance();
        
        return enableCacheCheckBox.isSelected() != settings.isEnableCache() ||
               !maxCacheSizeSpinner.getValue().equals(settings.getMaxCacheSize()) ||
               !maxSearchResultsSpinner.getValue().equals(settings.getMaxSearchResults()) ||
               caseSensitiveSearchCheckBox.isSelected() != settings.isCaseSensitiveSearch() ||
               includeDeprecatedCheckBox.isSelected() != settings.isIncludeDeprecated() ||
               autoRefreshCheckBox.isSelected() != settings.isAutoRefresh() ||
               !refreshIntervalSpinner.getValue().equals(settings.getRefreshInterval());
    }
    
    public void apply() throws ConfigurationException {
        // 验证设置值
        validateSettings();
        
        // 应用设置
        RestfulToolXSettings settings = RestfulToolXSettings.getInstance();
        settings.setEnableCache(enableCacheCheckBox.isSelected());
        settings.setMaxCacheSize((Integer) maxCacheSizeSpinner.getValue());
        settings.setMaxSearchResults((Integer) maxSearchResultsSpinner.getValue());
        settings.setCaseSensitiveSearch(caseSensitiveSearchCheckBox.isSelected());
        settings.setIncludeDeprecated(includeDeprecatedCheckBox.isSelected());
        settings.setAutoRefresh(autoRefreshCheckBox.isSelected());
        settings.setRefreshInterval((Integer) refreshIntervalSpinner.getValue());
    }
    
    public void reset() {
        RestfulToolXSettings settings = RestfulToolXSettings.getInstance();
        
        enableCacheCheckBox.setSelected(settings.isEnableCache());
        maxCacheSizeSpinner.setValue(settings.getMaxCacheSize());
        maxSearchResultsSpinner.setValue(settings.getMaxSearchResults());
        caseSensitiveSearchCheckBox.setSelected(settings.isCaseSensitiveSearch());
        includeDeprecatedCheckBox.setSelected(settings.isIncludeDeprecated());
        autoRefreshCheckBox.setSelected(settings.isAutoRefresh());
        refreshIntervalSpinner.setValue(settings.getRefreshInterval());
        
        // 更新UI状态
        refreshIntervalSpinner.setEnabled(autoRefreshCheckBox.isSelected());
    }
    
    private void validateSettings() throws ConfigurationException {
        // 验证缓存大小
        Integer cacheSize = (Integer) maxCacheSizeSpinner.getValue();
        if (cacheSize < 100 || cacheSize > 10000) {
            throw new ConfigurationException("Cache size must be between 100 and 10000");
        }
        
        // 验证搜索结果数
        Integer searchResults = (Integer) maxSearchResultsSpinner.getValue();
        if (searchResults < 10 || searchResults > 1000) {
            throw new ConfigurationException("Maximum search results must be between 10 and 1000");
        }
        
        // 验证刷新间隔
        Integer refreshInterval = (Integer) refreshIntervalSpinner.getValue();
        if (refreshInterval < 5 || refreshInterval > 300) {
            throw new ConfigurationException("Refresh interval must be between 5 and 300 seconds");
        }
    }
}